const router = require('koa-router')()
const {
	getBlogs,
	getBlogDetail,
	editBlog
} = require('../db/function/getblogs.js')
const {
	sendEmailF
} = require('../db/function/sendEmail')


router.get('/', async (ctx, next) => {
	let a = process.env.NODE_ENV
	await ctx.render('index', {
		title: 'Hello Koa 2!' + JSON.stringify(a)
	})
})

// 获取博客(不用登录)
router.post('/getblogs', async (ctx, next) => {
	let {
		pageSize,
		pageNo
	} = ctx.request.body
	if (!pageNo) pageNo = 1
	if (!pageSize) pageSize = 10
	console.log(pageSize);
	let res = await getBlogs({
		pageSize,
		pageNo
	})
	if (pageSize && pageNo)
		ctx.succ(res)
	else
		ctx.fail('请求失败')
})
// 获取博客详情(不用登录)
router.post('/getblogDetail', async (ctx, next) => {
	let {
		id
	} = ctx.request.body
	console.log(id);
	let res = await getBlogDetail(
		id
	)
	if (id)
		ctx.succ(res)
	else
		ctx.fail('请求失败')
})



router.post('/sendemail', async (ctx, next) => {
	let {
		name,
		phone,
		content
	} = ctx.request.body
	console.log(name, phone, content);
	if (name && phone && content) {
		// 执行
		let res = await sendEmailF(name, phone, content)
		console.log('rrreeesss', res);
		ctx.succ(res)
		if (res.indexOf('250 OK') >= 0) {
			ctx.succ('发送成功')
		} else {
			ctx.fail('发送失败')
		}
	}
	// let res = await getBlogDetail(
	// 	id
	// )
	// if (id)
	// 	ctx.succ(res)
	// else
	// 	ctx.fail('请求失败')
})
module.exports = router
